<!-- 承运商绑定坦克罐 -->
<template>
  <el-dialog
    title="绑定坦克罐"
    :visible.sync="dialogVisible"
    append-to-body
    width="60%"
  >
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @row-click="rowClick"
      @selection-change="selectionChange"
    >
    </avue-crud>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">关 闭</el-button>
      <el-button type="primary" v-noMoreClick @click="binding()"
        >绑 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { getList } from "@/api/basedata/tank";
import { bindOrUnBind } from "@/api/basedata/carriers";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: "auto",
        calcHeight: 200,
        tip: false,
        searchShow: false,
        searchMenuSpan: 6,
        border: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        menu: false,
        selection: true,
        labelWidth: "180",
        menuWidth: "250",
        column: [
          {
            label: "产品",
            prop: "product",
            search: true,
          },
          {
            label: "罐号",
            prop: "tankNumber",
            search: true,
          },
          {
            label: "容积",
            prop: "volume",
          },
        ],
      },
      data: [],
      selectRow: "", //选择数据
      selection: [], //选择数据
      dialogVisible: false,
      carrierId: "", //承运商id
    };
  },
  components: {},
  computed: {},
  methods: {
    searchReset() {
      this.query = {};
      this.getEscortList(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.getEscortList(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getEscortList(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getEscortList(this.page);
    },
    refreshChange() {
      this.getEscortList(this.page, this.query);
    },
    open(id) {
      this.dialogVisible = true;
      // this.selectRow = "";
      this.selection = [];
      this.carrierId = id;
      this.getEscortList(this.page);
    },
    getEscortList(page, params = {}) {
      let query = Object.assign(params, this.query);
      this.loading = true;
      getList(page.currentPage, page.pageSize, {
        ...query,
        carrierId: this.carrierId,
      }).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crud.$refs.table.doLayout();
        });
      });
    },
    //单选
    rowClick(row) {
      // this.selectRow = row.id;
      this.$refs.crud.toggleRowSelection(row);
    },
    //多选
    selectionChange(list) {
      this.selection = list;
    },
    //绑定
    binding() {
      if (this.selection.length === 0) {
        this.$message({
          message: "请选择要绑定的坦克罐",
          type: "warning",
        });
        return false;
      }
      bindOrUnBind({
        carrierId: this.carrierId,
        type: "tank",
        bindType: "bind",
        bindId: this.selection.map((item) => {
          return item.id;
        }),
      }).then(
        () => {
          this.dialogVisible = false;
          this.$emit("refresh");
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
        }
      );
    },
  },
};
</script>

<style></style>
